﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.woodsoft.core.Files;
using EQ1.Elements;
using System.Xml;
using com.woodsoft.core;

namespace EQ1.Files {
  public class SIDL : UIDocument {
    public SIDL ( string path )
      : base( path ) {
      this.Initialize();
    }

    private void Initialize () {
      XML par = new XML( this );
      UIElement schema = new UIElement( "" , "Schema" , "" , ( UIDocument ) par.OwnerDocument );
      schema.AddAttribute( "xmlns" , "EverQuestData" );
      schema.AddAttribute( "xmlns:dt" , "EverQuestDataTypes" );
      schema.AppendChild( CreateClass( schema ) );
      schema.AppendChild( CreateRGB( schema ) );
      schema.AppendChild( CreatePoint( schema ) );
      schema.AppendChild( CreateSize( schema ) );
      //schema.AppendChild( CreateTextureInfo( schema ) );
      
      par.AppendChild( schema );
      this.AppendChild( par );
    }

    private UIElement  CreateClass ( UIElement par ) {
      UIElement el = LoadElementType( par , "Class" );
      el.AppendChild( LoadAttribute( el , "item" , "string" ) );
      return el;
    }
    private UIElement CreateRGB ( UIElement par ) {
      UIElement el = LoadElementType( par , "RGB" );
      el.AppendChild( LoadSuperType( el , "Class" ) );
      el.AppendChild( LoadElement( el , "Alpha" , "int" , "255" ) );
      el.AppendChild( LoadElement( el , "R" , "int" , "0" ) );
      el.AppendChild( LoadElement( el , "G" , "int" , "0" ) );
      el.AppendChild( LoadElement( el , "B" , "int" , "0" ) );
      return el;
    }
    private UIElement CreatePoint ( UIElement par ) {
      UIElement el = LoadElementType( par , "Point" );
      el.AppendChild( LoadElement( el , "X" , "int" , "0" ) );
      el.AppendChild( LoadElement( el , "Y" , "int" , "0" ) );
      return el;
    }
    private UIElement CreateSize ( UIElement par ) {
      UIElement el = LoadElementType( par , "Size" );
      el.AppendChild( LoadElement( el , "CX" , "int" , "0" ) );
      el.AppendChild( LoadElement( el , "CY" , "int" , "0" ) );
      return el;
    }
    //private UIElement CreateTextureInfo ( UIElement par ) {
    //  UIElement el = LoadElementType( par , "TextureInfo" );
    //  el.AppendChild( LoadSuperType( el , "Class" ) );
    //  el.AppendChild( LoadElement( el , "Size" , "Size" , null ) );
    //}

    private UIElement LoadElementType ( UIElement par , string name ) {
      UIElement eltype = new UIElement( "" , "ElementType" , "" , ( UIDocument ) par.OwnerDocument );
      eltype.AddAttribute( "name" , name );
      return eltype;
    }
    private UIElement LoadAttribute ( UIElement par , string name , string type ) {
      UIElement el = new UIElement( "" , "attribute" , "" , ( UIDocument ) par.OwnerDocument );
      el.AddAttribute( "name" , name );
      el.AddAttribute( "type" , type );
      return el;
    }
    private UIElement LoadSuperType ( UIElement par , string type ) {
      UIElement el = new UIElement( "" , "superType" , "" , ( UIDocument ) par.OwnerDocument );
      el.AddAttribute( "type" , type );
      return el;
    }
    private UIElement LoadElement ( UIElement par , string name , string type , string value ) {
      UIElement el = new UIElement( "" , "element" , "" , ( UIDocument ) par.OwnerDocument );
      el.AddAttribute( "name" , name );
      el.AddAttribute( "type" , type );
      if( !string.IsNullOrEmpty( value ) ) {
        el.AppendChild( new UIElement( "" , "default" , "" , ( UIDocument ) el.OwnerDocument ) {
          InnerText = value
        } );
      }
      return el;
    }
  }
}
